﻿using System;
using System.IO.IsolatedStorage;
using System.Diagnostics;
using System.Collections.Generic;

namespace CloudTaobao.Util
{
    public class AppSettings
    {
        // Our isolated storage settings
        IsolatedStorageSettings isolatedStore;

        // The isolated storage key names of our settings
        const string nickName = "nick";
        const string appSecret = "secret";
        const string tokenName = "token";
        const string appKey = "top_appkey";
        const string session = "top_session";
        const string validSession = "valid";
        const string firstTime = "first";
        const string imeiName = "imei";
        const string imsiName = "imsi";
        const string isShowPicNoWIFIName = "isShowPicNoWIFI";
        const string recordsNoWIFIName = "recordsNoWIFI";
        const string recordsName = "records";
        const string queryName = "query";

        // The default value of our settings
        const string nickNameDefault = "";
        const string tokenNameDefault = "";
        const string sessionDefault = "";
        const bool firstDefault = true;
        const bool validSessionDefault = false;
        const string imeiDefault = "";
        const string imsiDefault = "";
        const bool isShowPicNoWIFIDefault = false;
        const int recordsNoWIFIDefault = 10;
        const int recordsDefault = 10;
        const string queryDefault = "windows phone";

        /// <summary>
        /// Constructor that gets the application settings.
        /// </summary>
        public AppSettings()
        {
            try
            {
                // Get the settings for this application.
                isolatedStore = IsolatedStorageSettings.ApplicationSettings;

            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }

        /// <summary>
        /// Update a setting value for our application. If the setting does not
        /// exist, then add the setting.
        /// </summary>
        /// <param name="Key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (isolatedStore.Contains(Key))
            {
                // If the value has changed
                if (isolatedStore[Key] != value)
                {
                    // Store the new value
                    isolatedStore[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                isolatedStore.Add(Key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        /// <summary>
        /// Get the current value of the setting, or if it is not found, set the 
        /// setting to the default setting.
        /// </summary>
        /// <typeparam name="valueType"></typeparam>
        /// <param name="Key"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public valueType GetValueOrDefault<valueType>(string Key, valueType defaultValue)
        {
            valueType value;

            // If the key exists, retrieve the value.
            if (isolatedStore.Contains(Key))
            {
                value = (valueType)isolatedStore[Key];
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }

            return value;
        }

        /// <summary>
        /// Save the settings.
        /// </summary>
        public void Save()
        {
            isolatedStore.Save();
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public string top_session
        {
            get
            {
                return GetValueOrDefault<string>(session, sessionDefault);
            }
            set
            {
                AddOrUpdateValue(session, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a ListBox Setting Key.
        /// </summary>
        public string token
        {
            get
            {
                return GetValueOrDefault<string>(tokenName, tokenNameDefault);
            }
            set
            {
                AddOrUpdateValue(tokenName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public string nick
        {
            get
            {
                return GetValueOrDefault<string>(nickName, nickNameDefault);
            }
            set
            {
                AddOrUpdateValue(nickName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public bool valid
        {
            get
            {
                return GetValueOrDefault<bool>(validSession, validSessionDefault);
            }
            set
            {
                AddOrUpdateValue(validSession, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public bool first
        {
            get
            {
                return GetValueOrDefault<bool>(firstTime, firstDefault);
            }
            set
            {
                AddOrUpdateValue(firstTime, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public string imei
        {
            get
            {
                return GetValueOrDefault<string>(imeiName, imeiDefault);
            }
            set
            {
                AddOrUpdateValue(imeiName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public string imsi
        {
            get
            {
                return GetValueOrDefault<string>(imsiName, imsiDefault);
            }
            set
            {
                AddOrUpdateValue(imsiName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public bool isShowPicNoWIFI
        {
            get
            {
                return GetValueOrDefault<bool>(isShowPicNoWIFIName, isShowPicNoWIFIDefault);
            }
            set
            {
                AddOrUpdateValue(isShowPicNoWIFIName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public int recordsNoWIFI
        {
            get
            {
                return GetValueOrDefault<int>(recordsNoWIFIName, recordsNoWIFIDefault);
            }
            set
            {
                AddOrUpdateValue(recordsNoWIFIName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public int records
        {
            get
            {
                return GetValueOrDefault<int>(recordsName, recordsDefault);
            }
            set
            {
                AddOrUpdateValue(recordsName, value);
                Save();
            }
        }

        /// <summary>
        /// Property to get and set a Username Setting Key.
        /// </summary>
        public string query
        {
            get
            {
                return GetValueOrDefault<string>(queryName, queryDefault);
            }
            set
            {
                AddOrUpdateValue(queryName, value);
                Save();
            }
        }
    }
}
